<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/8
 * Time: 19:06
 */

namespace app\common\model;


use think\Model;

class User extends Model
{


    public function getLists(){
        $depart_id = input('depart_id');
        $res = get_userlist_bydepartmentid($depart_id);
        if($res['errcode']==0){
            $list['data'] = $res['userlist'];
        }
        $list['code'] = 0;//用于前台数据表格判断成功字段
        return $list;
    }

    public function get_list(){
        $limit = input('limit');
        $user_id = input('user_id');
        //如果有user_id  则是需要筛选用户或中部门的信息
        if($user_id) {
            //user_id 是数字则是筛选部门信息
            if (is_numeric($user_id)) {
                $userlist = model('userlist')->where(['depart_id' => $user_id])->column('id','name');
                $uwhere['user_id'] = ['in', $userlist];
            } else {
                $uwhere['user_id'] = $user_id;
            }
            $list['data']['a'] = $this->field('sum(`integral`) as integral')->where($uwhere)->find();
            $list['data']['a']['user_name'] = model('userlist')->where(['id'=>$user_id])->value('name');
            $list['data']['a']['user_id'] = $user_id;
            if(is_numeric($user_id)){
               $array = $this
                    ->alias('t')
                    ->join('userlist u','t.user_id=u.id')
                    ->field('t.*,u.name')
                    ->where(['t.user_id'=>['in',$userlist]])
                    ->select()->toArray();
                $list['data'] =$array+$list['data'];

            }
            $list['current_page']=1;
            $list['last_page']=1;
            $list['per_page']=1;
            $list['total']=1;

        }else{
            $list = $this->field('user_id,user_name,integral')->order('integral desc')->paginate($limit)->toArray();
        }
        $list['code']=0;
        return $list;
    }
    /** 积分变化+积分日志
     * @param $type
     * @param $integral
     * @param $relation_id
     * @return bool
     * @throws \think\Exception
     */
    public function put_integral($type,$integral,$relation_id,$userid=''){
        //增加积分
        if($userid==''){
            $userid=session('user_info')['userid'];
        }
        $res = $this->where(['user_id'=>$userid])->setInc('integral',$integral);
        if($res!==false){
            $integral_log = new IntegralLog();
            $integral_log->put_log($type,$integral,$relation_id,$userid);
            return true;
        }
        return false;
    }


    /**经验变化+日志
     * @param $type
     * @param $exp
     * @param $relation_id
     * @return bool
     * @throws \think\Exception
     */
    public function put_exp($type,$exp,$relation_id){
        //增加经验
        $res = $this->where(['user_id'=>session('user_info')['userid']])->setInc('exp',$exp);
        if($res!==false){
            $exp_log = new ExpLog();
            $exp_log->put_log($type,$exp,$relation_id);
            return true;
        }
        return false;
    }
}